﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MisselCommand.Missile;


namespace MisselCommand.Missile
{
    public class MisselManager
    {
        //explosion
        private static LinkedList<Explosion> _explosion = new LinkedList<Explosion>();
        private static List<Explosion> _explosionToRemove = new List<Explosion>();
        //missels
        private static LinkedList<Missile> _missel = new LinkedList<Missile>();
        private static List<Missile> _missilesToRemove = new List<Missile>();

        private static ContentManager _content;

        public MisselManager(ContentManager content)
        {
            _content = content;
        }
        public static void AddMissel(Vector2 start, Vector2 end,Missile type)
        {
            Missile missel = type;
            missel.Load(start, end, _content);
            _missel.AddLast(missel);
        }
        public static void AddExplosion(Vector2 location, float maxwidth)
        {
            _explosion.AddLast(new Explosion(location, maxwidth, _content));
        }

        public void Update()
        {

            foreach (Missile m in _missel)
            {
                m.Update();
                if (m.DeathState)
                {
                    _missilesToRemove.Add(m);
                }
                foreach (Explosion e in _explosion)
                {
                    if (Vector2.Distance(e.GetLocation, m.GetLocation)<( e.Width / 2))
                    {
                        m.DeathState = true;
                    }
                }
            }
            foreach (Explosion e in _explosion)
            {
                e.Update();
                if (e.Dead)
                {
                    _explosionToRemove.Add(e);
                }
            }

            for (int i = 0; i < _missilesToRemove.Count; i++)
            {
                _missilesToRemove[i].Collision();
                _missel.Remove(_missilesToRemove[i]);
            }
            for (int i = 0; i < _explosionToRemove.Count; i++)
            {
                _explosion.Remove(_explosionToRemove[i]);
            }
            _missilesToRemove.Clear();
            _explosionToRemove.Clear();
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Missile m in _missel)
            {
                m.Draw(spriteBatch);
            }
            foreach (Explosion e in _explosion)
            {
                e.Draw(spriteBatch);
            }
        }

    }
}
